package com.sjq.supplier.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sjq.supplier.entity.Accomplish;
import com.sjq.supplier.entity.Order;
import com.sjq.supplier.entity.Warehouseinfo;
import com.sjq.supplier.service.AccomplishService;
import com.sjq.supplier.service.OrderService;
import com.sjq.supplier.service.WarehouseinfoService;
import com.sjq.supplier.util.Result;
import lombok.extern.slf4j.Slf4j;
import com.sjq.supplier.entity.Logistics;
import com.sjq.supplier.service.LogisticsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Logistics)表控制层
 *
 * @author makejava
 * @since 2024-01-02 14:54:51
 */
@Slf4j
@RestController
@RequestMapping("logistics")
public class LogisticsController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsService logisticsService;
    @Resource
    private AccomplishService accomplishService;
    @Resource
    private OrderService orderService;
    @Resource
    private WarehouseinfoService warehouseinfoService;

    /**
     * 分页查询所有数据
     *
     * @param logistics 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public Result selectAll(Logistics logistics, Integer limit, Integer page,Integer warehouseid) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        return logisticsService.selectAll(logistics,limit,page,warehouseid);
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @PostMapping("add")
    @Transactional(rollbackFor = Exception.class)
    public Result insert(Integer warehouseid, Integer accomplishid) {
        //判断两个参数是否为空
        if (warehouseid == null || accomplishid == null) {
            return Result.failure("参数错误");
        }

        //查询出这个物流信息
        Accomplish byId = accomplishService.getById(accomplishid);

        //赋值交付表
        Logistics logistics = new Logistics();
        logistics.setIsvalid(1);
        logistics.setGoodsid(byId.getGoodsid());
        logistics.setLogisticsname(byId.getLogisticsname());
        logistics.setLogisticsnum(byId.getLogisticsnum());
        logistics.setOrderid(byId.getOrderid());
        logistics.setPrice(byId.getPrice());
        logistics.setSumprice(byId.getSumprice());
        logistics.setSupplierid(byId.getSupplierid());
        logistics.setTitle(byId.getTitle());
        logistics.setImg(byId.getImg());
        logistics.setStock(byId.getStock());
        logistics.setUserid(byId.getUserid());

        //保存交付表
        if (logisticsService.save(logistics)){
            //根据订单号修改订单状态
            UpdateWrapper<Order> orderUpdateWrapper = new UpdateWrapper<>();
            orderUpdateWrapper.eq("orderid",byId.getOrderid());
            orderUpdateWrapper.set("state",2);
            orderService.update(null,orderUpdateWrapper);

            //入库操作
            Warehouseinfo warehouseinfo = new Warehouseinfo();
            warehouseinfo.setGoodsid(logistics.getGoodsid());
            warehouseinfo.setImg(logistics.getImg());
            warehouseinfo.setPrice(logistics.getPrice());
            warehouseinfo.setStock(logistics.getStock());
            warehouseinfo.setWarehouseid(warehouseid);
            warehouseinfo.setTitle(logistics.getTitle());
            warehouseinfo.setIsvalid(1);
            warehouseinfo.setUserid(logistics.getUserid());
            warehouseinfo.setOrderid(logistics.getOrderid());
            warehouseinfoService.save(warehouseinfo);
            return Result.success("签收并入库成功");
        }
        return Result.failure("签收失败");
    }


}

